package com.sxfq.loansupermarket.backsystem.modular.report.controller;

import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.CheckoutMethodInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.SearchCashDailyReportInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 现金日报
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/cashDailyReport")
public class CashDailyReportController extends BaseController {
    @Autowired
    private SqlMapper sqlMapper;

    private String PREFIX = "/report/wholeHospital/";

    /**
     * 跳转到现金日报页面
     */
    @RequestMapping("")
    public String cashDailyReport(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        String tableName = tableHead + "recharge_info";
        String sql = "update " + tableName + " a set a.recharges_time=left( DATE_ADD(a.recharge_time,INTERVAL 1 DAY) ,10) " +
                " where right(a.recharge_time,8)>= '" + ShiroKit.getUser().getSttleAccountsTime() + "' and a.recharges_time is null;";
        sqlMapper.update(sql);
        String sql2 = "update " + tableName + " a set a.recharges_time=left(a.recharge_time,10) " +
                " where right(a.recharge_time,8)<'" + ShiroKit.getUser().getSttleAccountsTime() + "' and a.recharges_time is null;";
        sqlMapper.update(sql2);
        model.addAttribute("sttleAccountsTime", ShiroKit.getUser().getSttleAccountsTime());
        return PREFIX + "cash_daily_report.html";
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public List<Map<String, Object>> report(SearchCashDailyReportInfo searchInfo, String obj) {
        String tableHead = searchInfo.getAbbreviation();
        String tableName1 = tableHead + "recharge_info";
        if ("hospital".equals(obj)) {
            String sqlj="";
            if (searchInfo.getStartTime() != null && searchInfo.getStartTime() != "") {
                sqlj += " and t.recharge_time >= '" + searchInfo.getStartTime() + "' ";
            }
            if (searchInfo.getEndTime() != null && searchInfo.getEndTime() != "") {
                sqlj += " and t.recharge_time < '" + searchInfo.getEndTime() + "' ";
            }
            String aa="";
            if (searchInfo.getEquation()==2) {
                aa= "*(a.equation/100)";
            }
            String getsql = "select t.checkout_method from " + tableName1 + " t left join checkout_method_info t1 on t.checkout_method_id=t1.id " +
                    "where 1=1 "+sqlj+" and t1.charge_bill_is_inCash=1 group by t.checkout_method order by t1.num ";
            List<CheckoutMethodInfo> list1 = sqlMapper.selectList(getsql, CheckoutMethodInfo.class);
            String timetpye;
            if ("0".equals(searchInfo.getSettleAccounts())) {
                timetpye = " left(a.recharge_time,10) ";
            } else {
                timetpye = " a.recharges_time ";
            }
            String sql = "select " + timetpye + " as rechargeTime,";
            if (list1.size() > 0) {
                for (int i = 0; i < list1.size(); i++) {
                    sql += " sum(case when a.checkout_method='" + list1.get(i).getCheckoutMethod() + "' then  a.actual_account_balance"+aa+" else 0 end) as '" + list1.get(i).getCheckoutMethod() + "',";
                }
            }
            sql += " sum(case when a.checkout_method in (" + getsql + ") then  a.actual_account_balance"+aa+" else 0 end) as totalDailyIncome from " + tableName1 + " a where 1=1 ";
            if (searchInfo.getStartTime() != null && searchInfo.getStartTime() != "") {
                sql += " and a.recharge_time >= '" + searchInfo.getStartTime() + "' ";
            }
            if (searchInfo.getEndTime() != null && searchInfo.getEndTime() != "") {
                sql += " and a.recharge_time < '" + searchInfo.getEndTime() + "' ";
            }
            sql += " group by " + timetpye + " union all select '合   计' as rechargeTime,";
            if (list1.size() > 0) {
                for (int i = 0; i < list1.size(); i++) {
                    sql += " sum(case when a.checkout_method='" + list1.get(i).getCheckoutMethod() + "' then  a.actual_account_balance"+aa+" else 0 end) as '" + list1.get(i).getCheckoutMethod() + "',";
                }
            }
            sql += " sum(case when a.checkout_method in (" + getsql + ") then  a.actual_account_balance"+aa+" else 0 end) as totalDailyIncome from " + tableName1 + " a where 1=1 ";
            if (searchInfo.getStartTime() != null && searchInfo.getStartTime() != "") {
                sql += " and a.recharge_time >= '" + searchInfo.getStartTime() + "' ";
            }
            if (searchInfo.getEndTime() != null && searchInfo.getEndTime() != "") {
                sql += " and a.recharge_time < '" + searchInfo.getEndTime() + "' ";
            }
            List<Map<String, Object>> list = sqlMapper.selectList(sql);
            return list;
        }
        if ("departmental".equals(obj)) {
            String sqlj="";
            if (searchInfo.getStartTime() != null && searchInfo.getStartTime() != "") {
                sqlj += " and t.recharge_time >= '" + searchInfo.getStartTime() + "' ";
            }
            if (searchInfo.getEndTime() != null && searchInfo.getEndTime() != "") {
                sqlj += " and t.recharge_time < '" + searchInfo.getEndTime() + "' ";
            }
            String aa="";
            if (searchInfo.getEquation()==2) {
                aa= "*(a.equation/100)";
            }
            String getsql = "select t.checkout_method from " + tableName1 + " t left join checkout_method_info t1 on t.checkout_method_id=t1.id " +
                    "where 1=1 "+sqlj+" and ( t1.type=0  or t1.type=3)  group by t.checkout_method order by t1.num ";
            List<CheckoutMethodInfo> list1 = sqlMapper.selectList(getsql, CheckoutMethodInfo.class);
            String timetpye;
            if ("0".equals(searchInfo.getSettleAccounts())) {
                timetpye = " left(a.recharge_time,10) ";
            } else {
                timetpye = " a.recharges_time ";
            }
            String sql = "select " + timetpye + " as rechargeTime,";
            if (list1.size() > 0) {
                for (int i = 0; i < list1.size(); i++) {
                    sql += " sum(case when a.checkout_method='" + list1.get(i).getCheckoutMethod() + "' then  a.actual_account_balance"+aa+" else 0 end) as '" + list1.get(i).getCheckoutMethod() + "',";
                }
            }
            sql += " sum(case when a.checkout_method in (" + getsql + ") then  a.actual_account_balance"+aa+" else 0 end) as totalDailyIncome from " + tableName1 + " a where 1=1 and (a.isEarnes=1 or a.isEarnes=5) ";
            if (searchInfo.getStartTime() != null && searchInfo.getStartTime() != "") {
                sql += " and a.recharge_time >= '" + searchInfo.getStartTime() + "' ";
            }
            if (searchInfo.getEndTime() != null && searchInfo.getEndTime() != "") {
                sql += " and a.recharge_time < '" + searchInfo.getEndTime() + "' ";
            }
            sql += " group by " + timetpye + " union all select '合   计' as rechargeTime,";
            if (list1.size() > 0) {
                for (int i = 0; i < list1.size(); i++) {
                    sql += " sum(case when a.checkout_method='" + list1.get(i).getCheckoutMethod() + "' then  a.actual_account_balance"+aa+" else 0 end) as '" + list1.get(i).getCheckoutMethod() + "',";
                }
            }
            sql += " sum(case when a.checkout_method in (" + getsql + ") then  a.actual_account_balance"+aa+" else 0 end) as totalDailyIncome from " + tableName1 + " a where 1=1 and (a.isEarnes=1 or a.isEarnes=5) ";
            if (searchInfo.getStartTime() != null && searchInfo.getStartTime() != "") {
                sql += " and a.recharge_time >= '" + searchInfo.getStartTime() + "' ";
            }
            if (searchInfo.getEndTime() != null && searchInfo.getEndTime() != "") {
                sql += " and a.recharge_time < '" + searchInfo.getEndTime() + "' ";
            }
            List<Map<String, Object>> list = sqlMapper.selectList(sql);
            return list;
        } else {
            return null;
        }
    }
}